package com.theeviljames.coursework.receiver;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.PriorityQueue;
import java.util.Timer;

import com.theeviljames.coursework.client.RadioListener;
import com.theeviljames.coursework.sender.SenderProtocol;

public class Receiver {
	
	private PriorityQueue<Sample> samples = new PriorityQueue<Sample>();
	private ReceiverProtocol protocol = ReceiverProtocol.getInstance();
	private boolean transmit;
	private int delay;
	private int period;
	
	public Receiver() {
		try {
			Socket client = new Socket("localhost",7777);
			BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			DataOutputStream out = new DataOutputStream(client.getOutputStream());
			String serverMessage = in.readLine();
			while(protocol.parseMessageString(serverMessage)!=101){
				serverMessage = in.readLine();
			}
			out.writeBytes(protocol.getMessage(102)+"\n");
			while(protocol.parseMessageString(serverMessage)!=103){
				serverMessage = in.readLine();
			}
			//Connected to the server
			UserInputTask userInput = new UserInputTask(this);
			userInput.start();
			
			while(!transmit){
				Thread.yield();
			}
			out.writeBytes(protocol.getMessage(201)+"\n");
			serverMessage = in.readLine();
			while(protocol.parseMessageString(serverMessage)!=202){
				serverMessage = in.readLine();
			}
			parseTransmitStart(serverMessage);
			
			RadioListener radioListener = new RadioListener(this);
			Timer timer = new Timer();
			DisplayTask displayTask = new DisplayTask(this);
			timer.scheduleAtFixedRate(displayTask, delay, period);
			
			while(transmit){
				serverMessage = in.readLine();
				if(protocol.parseMessageString(serverMessage)==302){
					bleepSample(parseBleep(serverMessage));
				}
				Thread.yield();
			}
			out.writeBytes(protocol.getMessage(401)+"\n");
			radioListener = null;
			timer.cancel();
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	public synchronized void addSample(Sample sample){
		samples.add(sample);
	}
	
	public synchronized Sample removeSample(){
		return samples.poll();
	}

	public synchronized void bleepSample(int sampleNo){
		Sample s = new Sample(sampleNo, "BLEEP!");
		samples.remove(s);
		samples.add(s);
	}
	
	public synchronized void setTransmit(boolean transmit){
		this.transmit = transmit;
	}
	
	public void parseTransmitStart(String string){
		int a = string.indexOf("=");
		int b = string.indexOf(";");
		String delayString = string.substring(a+1, b);
		delay = Integer.parseInt(delayString);
		int c = string.substring(b+1).indexOf("=");
		int d = string.substring(b+1).indexOf(";");
		String periodString = string.substring((b+1)+c+1, (b+1)+d);
		period = Integer.parseInt(periodString);
	}
	
	public int parseBleep(String string){
		int a = string.indexOf("#");
		String number = string.substring(a+1);
		return Integer.parseInt(number);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Receiver receiver = new Receiver();
	}

}
